//
//  YRStatusRetweetView.swift
//  新浪微博
//
//  Created by Viper on 16/6/29.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
import SnapKit

class YRStatusRetweetView: UIView {
    
    //声明一个全局的原创微博底部约束的约束 供我们卸载
    var RetweetViewButtonConstraint : Constraint?
    // 供外界赋值
    var status:YRStauts?{
        
        didSet{
            
            contentLabel.text = status?.retweeted_status?.text
            
            //卸载约束
            RetweetViewButtonConstraint?.uninstall()
            //如果有配图
            if status?.retweeted_status?.pic_urls?.count > 0 {
                //显示图片
                pictureView.hidden = false
              //赋值
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                //更新约束
                self.snp_makeConstraints { (make) in
                    
                    RetweetViewButtonConstraint = make.bottom.equalTo(pictureView).offset(StatusMargin).constraint
                }
          
            }else if status?.retweeted_status?.pic_urls?.count <= 0 {
                //如果没有配图 隐藏图片 更新约束
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    RetweetViewButtonConstraint = make.bottom.equalTo(contentLabel).offset(StatusMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setuoUI()
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) in
            
            make.leading.top.equalTo(self).offset(StatusMargin)
            
        }
        //给自己添加约束
        self.snp_makeConstraints { (make) in
            
          RetweetViewButtonConstraint = make.bottom.equalTo(pictureView).offset(StatusMargin).constraint
        }
   
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo(contentLabel)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 设置视图
    
    private func setuoUI(){
        
        backgroundColor = RGB(237, green: 237, blue: 237)
    }
    
   //    MARK: - 懒加载控件
    
    private lazy var contentLabel : UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 2*StatusMargin)
    
    //配图
   
    private lazy var pictureView : YRPictureView = YRPictureView()
    
}
